

////////////////////////////////////////////////////////////////////////////////
// this script add as a C comment the file name and line number at the end of
// each line
//
// example: applied to file named myFile.txt which contain
// 		line 1
//      line 2
//      line 3
// it will modify this file to contain
// 		line 1	//myFile.txt:1
//      line 2	//myFile.txt:2
//      line 3	//myFile.txt:3
//
// this script is to be used with formatJavacOutput.JScript
////////////////////////////////////////////////////////////////////////////////


import java.util.*;
import java.io.*;
import java.lang.*;
import java.lang.reflect.Array;

class copyAndAnotate
{

    // --- verbose
	boolean verbose = false;
	boolean bigEndian = false;


    // --- LOG
    void LOG(String a)
    {
        System.out.println(a);
    }

    // --- ERR
    void ERR(String a)
    {
    	System.err.println("ERR::"+a);
    }

    // --- ASSERT
	void ASSERT(boolean cond, String a) throws Exception
    {
        if (cond)
        {
            ERR(a);
            throw new Exception(a);
        }
    }

    // --- write32BigEndian
	void write32BigEndian(FileOutputStream out, int value) throws Exception
    {
		byte [] b = new byte[4];
		b[0] = (byte)((value >> 24) & 0xFF);
		b[1] = (byte)((value >> 16) & 0xFF);
		b[2] = (byte)((value >> 8) & 0xFF);
		b[3] = (byte)(value & 0xFF);
		out.write(b, 0, 4);
	}

    // --- write16BigEndian
	void write16BigEndian(FileOutputStream out, int value) throws Exception
	{
		byte [] b = new byte[2];
		b[0] = (byte)((value >> 8) & 0xFF);
		b[1] = (byte)(value & 0xFF);
		out.write(b, 0, 2);
	}

	// --- write32LittleEndian
	void write32LittleEndian(FileOutputStream out, int value) throws Exception
	{
		byte [] b = new byte[4];
		b[0] = (byte)(value & 0xFF);
		b[1] = (byte)((value >> 8) & 0xFF);
		b[2] = (byte)((value >> 16) & 0xFF);
		b[3] = (byte)((value >> 24) & 0xFF);
		out.write(b, 0, 4);
	}

    // --- write16LittleEndian
	void write16LittleEndian(FileOutputStream out, int value) throws Exception
	{
		byte [] b = new byte[2];
		b[0] = (byte)(value & 0xFF);
		b[1] = (byte)((value >> 8) & 0xFF);
		out.write(b, 0, 2);
	}

	// --- write32
	void write32(FileOutputStream out, int value) throws Exception
	{
        if (bigEndian)
            write32BigEndian(out, value);
		else
			write32LittleEndian(out, value);
	}

	// --- write16
	void write16(FileOutputStream out, int value) throws Exception
	{
		if (bigEndian)
            write16BigEndian(out, value);
		else
				write16LittleEndian(out, value);
	}




















//void LOG(String a){System.out.println(a);}
//void ERR(String a){System.err.println(a);}


// get filepath given complete filename
String getFilePath(String filename)
{
	int i;
	if ( (i = filename.lastIndexOf('\\')) != -1) {
		return filename.substring(0, i) + "\\";

	} else if ( (i = filename.lastIndexOf('/')) != -1) {
		filename = filename.substring(0, i) + "/";
		filename = filename.replaceAll("\\", "/");
		return filename;
	} else {
		return ".\\";
	}
}

// get filename given complete filename
String getFileName(String filename)
{
	File f = new File(filename);
	if (f.getName().length() > 0)
		return f.getName();
	else
		return "dummy";
}


void CopyAndAddLineNumber(String filename, String dstDirectory)
{
	String s = null;
	StringBuffer src = new StringBuffer("\n");
	int linenumber = 0;
	int i, j;

	try {
		// open file
		LineNumberReader in = new LineNumberReader( new FileReader(filename) );
		linenumber = in.getLineNumber();

		// read each line and add line number
		do {
			s = in.readLine();
			if (s != null)
				src.append( s + "\t//" + filename + ":" + in.getLineNumber() + ":\n" );
			else
				src.append( "\t//" + filename + ":" + in.getLineNumber() + ":\n" );
		} while (s != null);
		in.close();
		src.append( "\n" );
		// save file
		String outFile = getFilePath(dstDirectory)+getFileName(filename);
		//FileWriter out = new FileWriter( filename );
		FileWriter out = new FileWriter( outFile );
		s = src.toString();
		out.write(s);
		out.close();

	} catch (FileNotFoundException e) {
		ERR("FILE NOT FOUND "+filename);

	} catch (Exception e) {
		ERR("unknown error" + e.toString());
	}
}




//System.out.println(" "+Array.getLength(args));

//if (args.length == 3) {
//	CopyAndAddLineNumber(args[1], args[2]);
//} else {
//	LOG("Syntax: java -jar BeanShell.jar "+args[1]+" file directory");
//	LOG("    copy file to directory, and add line number + src path of original file");
//}

String outDir = ".\\";

	void exec(String[] args) throws Exception
	{

		for (int i=0; i<args.length; i++) {
			if ((args[i].compareToIgnoreCase("-d") == 0) && (i+1<args.length)) {
				outDir = args[i+1];
				outDir = getFilePath(outDir);
				i++;

			} else if ((args[i].compareToIgnoreCase("-f") == 0) && (i+1<args.length)) {
				try {
					// open file
					i++;
					BufferedReader in = new BufferedReader( new FileReader(args[i]) );
					String s;

					do {
						s = in.readLine();

						if (s!= null)
							s.trim();
						if ((s != null) && (s.length() > 0))
							CopyAndAddLineNumber(s, outDir);

					} while (s != null);
					in.close();

				} catch (Exception e) {
					ERR("error while processing file:"+args[1]+":"+ e.toString());
				}

			} else {
		//		CopyAndAddLineNumber(args[i], outDir);
			}
		}
	}

	static public void main(String[] args)
	{
	    try {
	        copyAndAnotate m = new copyAndAnotate();
	        m.exec(args);
	    } catch(Exception e)
	    { }
	}

}